package com.ysd.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.R;
import com.ysd.modules.sys.entity.CourseEntity;
import com.ysd.modules.sys.entity.SysTeacher;
import com.ysd.modules.sys.entity.SysUserEntity;
import com.ysd.modules.sys.entity.TestPaper;
import com.ysd.modules.sys.model.AutoTopicReq;
import com.ysd.modules.sys.model.StatusReqs;
import com.ysd.modules.sys.service.CourseService;
import com.ysd.modules.sys.service.TestPaperService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dapeng
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/testPaper")
public class TestPaperController extends AbstractController {
    @Autowired
    private TestPaperService testPaperService;

    @Autowired
    private CourseService courseService;

    /**
     * 列表查询
     *
     * @param req
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("testPaper:list")
    public R listSearch(@RequestParam Map<String, Object> req) {
        IPage<TestPaper> page = testPaperService.listSearch(req);
        R ok = R.ok();
        ok.put("data", page.getRecords());
        ok.put("count", page.getTotal());
        return ok;
    }

    /**
     * 查询详情
     *
     * @param id
     * @return R
     */
    @GetMapping("/detail/search")
    @RequiresPermissions("testPaper:detail:search")
    public R listSearch(@RequestParam Integer id) {
        TestPaper testPaper = testPaperService.detailSearch(id);
        return R.ok().put("data", testPaper);
    }

    /**
     * 新增/修改
     *
     * @param req
     * @return R
     */
    @PostMapping("/save")
    @RequiresPermissions("testPaper:save")
    public R save(@RequestBody TestPaper req) {
        SysUserEntity user = this.getUser();
        this.verifyForm(req);
        if (req.getId() == null || 0 == req.getId()) {
            req.setType(0);
            req.setTopicNum(0);
            req.setCreateName(user.getUsername());
            req.setCreateId(user.getUserId());
            req.setCreateTime(new Date());
            testPaperService.save(req);
        } else {
            testPaperService.updateById(req);
        }
        return R.ok();
    }

    /**
     * 批量修改状态
     *
     * @param req
     * @return R
     */
    @PostMapping("/status/modify")
    @RequiresPermissions("testPaper:status:modify")
    public R statusModify(@RequestBody StatusReqs req) {
        testPaperService.statusModify(req.getReq());
        return R.ok();
    }


    @PostMapping("/auto/topic/save")
    @RequiresPermissions("testPaper:auto:topic:save")
    public R statusModify(@RequestBody List<AutoTopicReq> req) {
        testPaperService.autoTopic(req);
        return R.ok();
    }

    /**
     * 校验数据
     *
     * @param testPaper
     * @return
     */
    public void verifyForm(TestPaper testPaper) {
        if (StringUtils.isBlank(testPaper.getTitle())) {
            throw new RRException("试卷标题不能为空");
        }

        if (testPaper.getDuration() == null || 0 == testPaper.getDuration()) {
            throw new RRException("时长不能为空");
        }

        //菜单
        if (testPaper.getCourseId() == null || testPaper.getCourseId() == 0) {
            throw new RRException("课程不能为空");
        }

        CourseEntity course = courseService.getById(testPaper.getCourseId());
        if (course == null) {
            throw new RRException("课程不存在");
        }
        testPaper.setCourseName(course.getName());

        SysUserEntity user = this.getUser();
        testPaper.setCreateId(user.getUserId());
        testPaper.setCreateName(user.getUsername());
    }

    @PostMapping("addPaper")
    @ApiOperation("发起考试(目前只能发起课程,试卷可以后期优化)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "目标id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "chapters", value = "数组(章节id)", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型默认1(1课程/2试卷)", paramType = "query", dataType = "int")
    })
    public R addPaper(@RequestBody TestPaper testPaper, Integer classId, Integer[] chapters,Integer type) {
        int i = testPaperService.addPaper(testPaper, classId, chapters, type);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

